﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using QuanLyTruongHoc_App.MyDataSet;
using QuanLyTruongHoc_App.QLTHTableAdapters;

namespace QuanLyTruongHoc_App
{
    public partial class AddEditHocSinh : Form
    {
        private String filePath = "";
        private int currID = 0, idCMND, idBo, idMe, curFlag;
        private Boolean editMode = false;
        private DataRow editRow, cmndRow, boRow, meRow;

        public AddEditHocSinh()
        {
            InitializeComponent();
            cb_GioiTinh.SelectedIndex = 0;
            idCMND = -1;
            curFlag = PConst.FLAG_NONE;
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddEditGiaoVien_Load(object sender, EventArgs e)
        {
            refreshData();
            refreshID();
            tb_HoDem.Focus();
        }

        private void refreshID()
        {
            if (!editMode)
                currID = PublicLib.getMaxIdFromTbl(qLTH.tbl_HocSinh.TableName, qLTH.tbl_HocSinh.idColumn.ColumnName) + 1;
            tb_MaHS.Text = PublicLib.formatHSId(currID);
        }

        private void refreshData()
        {
            // Change Connection String
            this.tbl_NhapTruongTableAdapter.Connection.ConnectionString = 
                this.tbl_LopHocTableAdapter.Connection.ConnectionString = 
                PublicLib.getStrConn();
            // Fill Data
            this.tbl_LopHocTableAdapter.Fill(this.qLTH.tbl_LopHoc);
            this.tbl_NhapTruongTableAdapter.FillByFlag(this.qLTH.tbl_NhapTruong, PConst.FLAG_NONE);
        }

        private void tb_DoiAnh_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "JPG, JPEG FILE (*.jpg, *.jpeg)|*.jpg;*.jpeg";
            dialog.Multiselect = false;
            dialog.ShowDialog();

            filePath = dialog.FileName;

            if (filePath == "")
                return;
            FileInfo fi = new FileInfo(filePath);
            if (fi.Length > PConst.MAX_IMG_SIZE)
            {
                MessageBox.Show(MSG.MSG_IMAGE_TOO_BIG);
                filePath = "";
                return;
            }

            pb_Anh.Load(filePath);
        }

        public void setEditInfo(int _id)
        {
            try
            {
                DataRow[] newRow;
                MyDataSet.QLTHTableAdapters.tbl_HocSinhTableAdapter adap = 
                    new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_HocSinhTableAdapter();
                adap.Connection.ConnectionString = PublicLib.getStrConn();
                adap.Fill(qLTH.tbl_HocSinh);

                newRow = qLTH.tbl_HocSinh.Select(qLTH.tbl_HocSinh.idColumn.ColumnName + "=" + _id.ToString());
                if (newRow.Length == 0)
                    return;
                editRow = newRow[0];

                currID = _id;
                tb_HoDem.Text = editRow[qLTH.tbl_HocSinh.HoDemColumn].ToString();
                tb_Ten.Text = editRow[qLTH.tbl_HocSinh.TenColumn].ToString();
                dt_NgaySinh.Value = (DateTime)editRow[qLTH.tbl_HocSinh.NgaySinhColumn];
                tb_NoiSinh.Text = editRow[qLTH.tbl_HocSinh.NoiSinhColumn].ToString();

                cb_GioiTinh.SelectedIndex = int.Parse(editRow[qLTH.tbl_HocSinh.GioiTinhColumn].ToString());
                // CMND
                MyDataSet.QLTHTableAdapters.tbl_CMNDTableAdapter cmndAdap =
                    new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_CMNDTableAdapter();
                cmndAdap.Connection.ConnectionString = PublicLib.getStrConn();
                cmndAdap.Fill(qLTH.tbl_CMND);

                idCMND = (int)editRow[qLTH.tbl_HocSinh.idCMNDColumn];
                chk_CMND.Checked = (idCMND != -1);
                if (idCMND != -1)
                {
                    cmndRow = qLTH.tbl_CMND.Select(qLTH.tbl_CMND.idColumn + "=" + idCMND.ToString())[0];
                    dt_NgayCapCMND.Value = (DateTime)cmndRow[qLTH.tbl_CMND.NgayCapColumn];
                    tb_NoiCapCMND.Text = cmndRow[qLTH.tbl_CMND.NoiCapColumn].ToString();
                    tb_SoCMND.Text = cmndRow[qLTH.tbl_CMND.SoCMNDColumn].ToString();
                }
                // -------------
                tb_TonGiao.Text = editRow[qLTH.tbl_HocSinh.TonGiaoColumn].ToString();
                tb_DanToc.Text = editRow[qLTH.tbl_HocSinh.DanTocColumn].ToString();
                chk_TraiTuyen.Checked = (Boolean)editRow[qLTH.tbl_HocSinh.TraiTuyenColumn];

                tb_DiaChi.Text = editRow[qLTH.tbl_HocSinh.DiaChiColumn].ToString();
                tb_DienThoai.Text = editRow[qLTH.tbl_HocSinh.DienThoaiColumn].ToString();
                tb_Email.Text = editRow[qLTH.tbl_HocSinh.EmailColumn].ToString();
                cb_NhapTruong.SelectedValue = editRow[qLTH.tbl_HocSinh.NhapTruongColumn];
                dt_NgayNhapTruong.Value = (DateTime)editRow[qLTH.tbl_HocSinh.NgayNhapTruongColumn];

                cb_lop.SelectedValue = editRow[qLTH.tbl_HocSinh.idLopColumn];

                // Anh
                if (!(editRow[qLTH.tbl_HocSinh.AnhColumn] == DBNull.Value))
                {
                    filePath = System.IO.Path.GetTempFileName();
                    FileStream fs = new System.IO.FileStream(filePath, FileMode.Create, FileAccess.Write);
                    Byte []str = (byte[])editRow[qLTH.tbl_HocSinh.AnhColumn];
                    fs.Write(str, 0, str.Length);
                    fs.Close();

                    pb_Anh.Load(filePath);
                }
                else
                    filePath = "";

                // Bo ME
                MyDataSet.QLTHTableAdapters.tbl_ChaMeTableAdapter chaMeAdap = new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_ChaMeTableAdapter();
                chaMeAdap.Connection.ConnectionString = PublicLib.getStrConn();
                chaMeAdap.Fill(qLTH.tbl_ChaMe);

                idBo = (int)editRow[qLTH.tbl_HocSinh.idChaColumn];
                boRow = qLTH.tbl_ChaMe.Select(qLTH.tbl_ChaMe.idColumn + "=" + idBo)[0];

                tb_HoTenCha.Text = boRow[qLTH.tbl_ChaMe.HoTenColumn].ToString();
                tb_DienThoaiCha.Text = boRow[qLTH.tbl_ChaMe.DienThoaiColumn].ToString();
                tb_NgheNghiepCha.Text = boRow[qLTH.tbl_ChaMe.NgheNghiepColumn].ToString();
                tb_DiaChiCha.Text = boRow[qLTH.tbl_ChaMe.DiaChiColumn].ToString();
                //Me
                idMe = (int)editRow[qLTH.tbl_HocSinh.idMeColumn];
                meRow = qLTH.tbl_ChaMe.Select(qLTH.tbl_ChaMe.idColumn + "=" + idMe)[0];

                tb_HoTenMe.Text = meRow[qLTH.tbl_ChaMe.HoTenColumn].ToString();
                tb_DienThoaiMe.Text = meRow[qLTH.tbl_ChaMe.DienThoaiColumn].ToString();
                tb_NgheNghiepMe.Text = meRow[qLTH.tbl_ChaMe.NgheNghiepColumn].ToString();
                tb_DiaChiMe.Text = meRow[qLTH.tbl_ChaMe.DiaChiColumn].ToString();
                // End Bo Me
                tb_ThuongBinh.Text = editRow[qLTH.tbl_HocSinh.ConThuongBinhColumn].ToString();

                curFlag = (int)editRow[qLTH.tbl_HocSinh.FlagColumn];
                editMode = true;
                refreshID();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi :" + ex.Message);
            }
        }

        private Boolean checkInfo()
        {
            
            return true;
        }

        private void bt_ThemMoi_Click(object sender, EventArgs e)
        {
            if (!checkInfo())
                return;

            try
            {
                refreshID();

                if (!editMode)
                    editRow = qLTH.tbl_HocSinh.NewRow();

                editRow[qLTH.tbl_HocSinh.idColumn] = currID;
                editRow[qLTH.tbl_HocSinh.HoDemColumn] = tb_HoDem.Text;
                editRow[qLTH.tbl_HocSinh.TenColumn] = tb_Ten.Text;
                editRow[qLTH.tbl_HocSinh.NgaySinhColumn] = dt_NgaySinh.Value;
                editRow[qLTH.tbl_HocSinh.NoiSinhColumn] = tb_NoiSinh.Text;
                editRow[qLTH.tbl_HocSinh.GioiTinhColumn] = cb_GioiTinh.SelectedIndex;
                // CMND
                if (chk_CMND.Checked)
                {
                    Boolean needAdd = false;
                    if (!editMode || idCMND == -1)
                    {
                        cmndRow = qLTH.tbl_CMND.NewRow();
                        idCMND = PublicLib.getMaxIdFromTbl(qLTH.tbl_CMND.TableName, qLTH.tbl_CMND.idColumn.ColumnName) + 1;
                        needAdd = true;
                    }

                    cmndRow[qLTH.tbl_CMND.idColumn] = idCMND;
                    cmndRow[qLTH.tbl_CMND.NgayCapColumn] = dt_NgayCapCMND.Value;
                    cmndRow[qLTH.tbl_CMND.NoiCapColumn] = tb_NoiCapCMND.Text;
                    cmndRow[qLTH.tbl_CMND.SoCMNDColumn] = tb_SoCMND.Text;

                    MyDataSet.QLTHTableAdapters.tbl_CMNDTableAdapter 
                        cmndAdap = new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_CMNDTableAdapter();
                    cmndAdap.Connection.ConnectionString = PublicLib.getStrConn();

                    editRow[qLTH.tbl_HocSinh.idCMNDColumn] = idCMND;
                    if (!editMode || needAdd)
                        qLTH.tbl_CMND.Rows.Add(cmndRow);
                    
                    cmndAdap.Update(cmndRow);
                }
                else
                    editRow[qLTH.tbl_HocSinh.idCMNDColumn] = -1;
                // -------------
                editRow[qLTH.tbl_HocSinh.TonGiaoColumn] = tb_TonGiao.Text;
                editRow[qLTH.tbl_HocSinh.DanTocColumn] = tb_DanToc.Text;
                editRow[qLTH.tbl_HocSinh.TraiTuyenColumn] = chk_TraiTuyen.Checked;
                editRow[qLTH.tbl_HocSinh.DiaChiColumn] = tb_DiaChi.Text;
                editRow[qLTH.tbl_HocSinh.DienThoaiColumn] = tb_DienThoai.Text;
                editRow[qLTH.tbl_HocSinh.EmailColumn] = tb_Email.Text;

                editRow[qLTH.tbl_HocSinh.NhapTruongColumn] = cb_NhapTruong.SelectedValue;
                editRow[qLTH.tbl_HocSinh.NgayNhapTruongColumn] = dt_NgayNhapTruong.Value;

                editRow[qLTH.tbl_HocSinh.idLopColumn] = cb_lop.SelectedValue;
                // Lop Hien tai
                
                // ----------------------------------------
                // Anh
                if (!(filePath == ""))
                {
                    FileStream fs = new System.IO.FileStream(filePath, FileMode.Open, FileAccess.Read);
                    FileInfo fi = new FileInfo(filePath);
                    byte[] str = new byte[fi.Length];
                    long byteread = fs.Read(str, 0, (int)fi.Length);
                    editRow[qLTH.tbl_HocSinh.AnhColumn] = str;
                }
                else
                    editRow[qLTH.tbl_HocSinh.AnhColumn] = DBNull.Value;
                // -------------
                // Bo ME
                MyDataSet.QLTHTableAdapters.tbl_ChaMeTableAdapter chaMeAdap = new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_ChaMeTableAdapter();
                chaMeAdap.Connection.ConnectionString = PublicLib.getStrConn();

                if (!editMode)
                {
                    idBo = PublicLib.getMaxIdFromTbl(qLTH.tbl_ChaMe.TableName, qLTH.tbl_ChaMe.idColumn.ColumnName) + 1;
                    boRow = qLTH.tbl_ChaMe.NewRow();
                }

                boRow[qLTH.tbl_ChaMe.idColumn] = idBo;
                boRow[qLTH.tbl_ChaMe.HoTenColumn] = tb_HoTenCha.Text;
                boRow[qLTH.tbl_ChaMe.DienThoaiColumn] = tb_DienThoaiCha.Text;
                boRow[qLTH.tbl_ChaMe.NgheNghiepColumn] = tb_NgheNghiepCha.Text;
                boRow[qLTH.tbl_ChaMe.DiaChiColumn] = tb_DiaChiCha.Text;
                if (!editMode)
                    qLTH.tbl_ChaMe.Rows.Add(boRow);

                chaMeAdap.Update(boRow);
                editRow[qLTH.tbl_HocSinh.idChaColumn] = idBo;
                //Me
                if (!editMode)
                {
                    idMe = PublicLib.getMaxIdFromTbl(qLTH.tbl_ChaMe.TableName, qLTH.tbl_ChaMe.idColumn.ColumnName) + 1;
                    meRow = qLTH.tbl_ChaMe.NewRow();
                }

                meRow[qLTH.tbl_ChaMe.idColumn] = idMe;
                meRow[qLTH.tbl_ChaMe.HoTenColumn] = tb_HoTenMe.Text;
                meRow[qLTH.tbl_ChaMe.DienThoaiColumn] = tb_DienThoaiMe.Text;
                meRow[qLTH.tbl_ChaMe.NgheNghiepColumn] = tb_NgheNghiepMe.Text;
                meRow[qLTH.tbl_ChaMe.DiaChiColumn] = tb_DiaChiMe.Text;
                if (!editMode)
                    qLTH.tbl_ChaMe.Rows.Add(meRow);

                chaMeAdap.Update(meRow);
                editRow[qLTH.tbl_HocSinh.idMeColumn] = idMe;

                // End Bo Me
                editRow[qLTH.tbl_HocSinh.ConThuongBinhColumn] = tb_ThuongBinh.Text;

                editRow[qLTH.tbl_HocSinh.FlagColumn] = curFlag;

                MyDataSet.QLTHTableAdapters.tbl_HocSinhTableAdapter adap = new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_HocSinhTableAdapter();
                adap.Connection.ConnectionString = PublicLib.getStrConn();

                if (!editMode)
                {
                    qLTH.tbl_HocSinh.Rows.Add(editRow);
                }
                adap.Update(editRow);
                // update lop hoc
                if (curFlag == PConst.FLAG_NONE)
                {
                    int idNamHoc = PublicLib.getNamHocHienTai();
                    int idLop = (int)cb_lop.SelectedValue;
                    MyDataSet.QLTHTableAdapters.qh_LopHocSinhTableAdapter adapLopHS =
                        new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.qh_LopHocSinhTableAdapter();
                    adapLopHS.Connection.ConnectionString = PublicLib.getStrConn();
                    adapLopHS.FillByNamHocHocSinh(qLTH.qh_LopHocSinh, idNamHoc, currID);
                    if (qLTH.qh_LopHocSinh.Rows.Count > 0)
                    {
                        qLTH.qh_LopHocSinh.Rows[0][qLTH.qh_LopHocSinh.idLopColumn] = idLop;
                        adapLopHS.Update(qLTH.qh_LopHocSinh.Rows[0]);
                    }
                    else
                    {
                        DataRow dtRow = qLTH.qh_LopHocSinh.NewRow();
                        dtRow[qLTH.qh_LopHocSinh.idHocSinhColumn] = currID;
                        dtRow[qLTH.qh_LopHocSinh.idLopColumn] = idLop;
                        dtRow[qLTH.qh_LopHocSinh.idNamHocColumn] = idNamHoc;
                        qLTH.qh_LopHocSinh.Rows.Add(dtRow);

                        adapLopHS.Update(dtRow);
                    }
                }
                // -------------------

                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(MSG.MSG_ERROR_INSERT_DB + " :" + ex.Message);
            }
        }

        private void chk_CMND_CheckedChanged(object sender, EventArgs e)
        {
            pn_CMND.Enabled = chk_CMND.Checked;
        }

        private void bt_DSLop_Click(object sender, EventArgs e)
        {
            QLHocSinh frm_DSLop = new QLHocSinh();
            frm_DSLop.MdiParent = this.MdiParent;

            frm_DSLop.Show();
        }

        private void bt_XemHocBa_Click(object sender, EventArgs e)
        {
            if (!editMode) return;
            HocBa frm_HocBa = new HocBa(currID);
            frm_HocBa.MdiParent = this.MdiParent;

            frm_HocBa.Show();
        }

        private void bt_ChuyenLop_Click(object sender, EventArgs e)
        {
            cb_lop.Focus();
        }

        private void changeFlag(short _flag)
        {
            DataRow[] tmpRow = qLTH.tbl_HocSinh.Select(qLTH.tbl_HocSinh.idColumn.ColumnName + "=" + currID);
            if (tmpRow.Length > 0)
            {
                tmpRow[0][qLTH.tbl_HocSinh.FlagColumn] = _flag;
                MyDataSet.QLTHTableAdapters.tbl_HocSinhTableAdapter adap = new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_HocSinhTableAdapter();
                adap.Connection.ConnectionString = PublicLib.getStrConn();

                adap.Update(tmpRow[0]);
                this.Close();
            }
        }

        private void bt_ChuyenTruong_Click(object sender, EventArgs e)
        {
            if (!editMode) return;

            if (MessageBox.Show(MSG.MSG_CHUYEN_TRUONG, "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                changeFlag(PConst.FLAG_CHUYEN_TRUONG);
            }
        }

        private void bt_XoaHoSo_Click(object sender, EventArgs e)
        {
            if (!editMode) return;

            if (MessageBox.Show(MSG.MSG_DELETE, "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                changeFlag(PConst.FLAG_DELETE);
            }
        }

        private void bt_IN_Click(object sender, EventArgs e)
        {
            PrintDataGrid.PrintHoSo p = new PrintDataGrid.PrintHoSo();
            p.Title = "Học Sinh " + tb_HoDem.Text + " " + tb_Ten.Text;
            p.setImage(filePath);

            p.addField("", "THÔNG TIN CƠ BẢN");
            p.addField("Mã Học Sinh", tb_MaHS.Text);
            p.addField("Họ tên", tb_HoDem.Text + " " + tb_Ten.Text);
            p.addField("Ngày Sinh", dt_NgaySinh.Value.ToString("dd/MM/yyyy"));
            p.addField("Giới Tính", cb_GioiTinh.Text);
            p.addField("Nơi Sinh", tb_NoiSinh.Text);

            if (chk_CMND.Checked)
            {
                p.addField("Số CMND", tb_SoCMND.Text);
                p.addField("Ngày cấp", dt_NgayCapCMND.Value.ToString("dd/MM/yyyy"));
                p.addField("Nơi Cấp", tb_NoiCapCMND.Text);
            }

            p.addField("Dân tộc", tb_DanToc.Text);
            if (chk_TraiTuyen.Checked)
                p.addField("Trái tuyến", "Học sinh trái tuyến");
            else
                p.addField("Trái tuyến", "Không");

            p.addField("Tôn Giáo", tb_TonGiao.Text);
            p.addField("Địa chỉ", tb_DiaChi.Text);
            p.addField("Điện thoại", tb_DienThoai.Text);
            p.addField("Email", tb_Email.Text);

            p.addField("", "");
            p.addField(PrintDataGrid.PrintHoSo.PAGE_BREAK, "");

            p.addField("", "THÔNG TIN GIA ĐÌNH");
            p.addField("Họ tên bố", tb_HoTenCha.Text);
            p.addField("Nghề nghiệp", tb_NgheNghiepCha.Text);
            p.addField("Địa chỉ", tb_DiaChiCha.Text);
            p.addField("Điện thoại", tb_DienThoaiCha.Text);

            p.addField("", "" );
            p.addField("Họ tên mẹ", tb_HoTenMe.Text);
            p.addField("Nghề nghiệp", tb_NgheNghiepMe.Text);
            p.addField("Địa chỉ", tb_DiaChiMe.Text);
            p.addField("Điện thoại", tb_DienThoaiMe.Text);

            PrintDataGrid.PrintHoSoOptions opt = new PrintDataGrid.PrintHoSoOptions(p);
            opt.MdiParent = this.MdiParent;
            opt.Show();
        }


    }
}
